package fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.boltshow.R;
import com.example.administrator.boltshow.SpecialActivity;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.HotRVAdapter;
import config.NetConfig;
import model.HotMonkeyModel;

/**
 * Created by Administrator on 2016/8/29.
 */
public class HotFragment extends Fragment implements View.OnClickListener {

    private RecyclerView mRecyclerView;
    private HotRVAdapter mAdapter;
    private List<HotMonkeyModel> mData;
    private String[] mPath={NetConfig.BIG_MONKEY_MOUSE,NetConfig.MICHAEL_WATCH,
            NetConfig.SONG_LOCAL,NetConfig.SPECIAL_CAR,NetConfig.SEA_PATH,
            NetConfig.PRINCE_PATH,NetConfig.BOY_AND_GIRL,NetConfig.SEA_PATH,
            NetConfig.TOBE_NUMBER_ONE,NetConfig.FRIEND_SON,NetConfig.FLOWER,
            NetConfig.Y_S_L};
    private LinearLayoutManager mManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.hot_fragment,null);

        initView(view);
        initData();
        setData();

        return view;
    }

    private void setData() {
        mManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);

    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new HotRVAdapter(mData,getActivity(),this);

        for (int i = 0; i < mPath.length; i++) {
            x.http().get(new RequestParams(mPath[i]), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Gson gson = new Gson();
                    HotMonkeyModel hotMonkeyModel = gson.fromJson(result, HotMonkeyModel.class);
                    mData.add(hotMonkeyModel);
                    mAdapter.notifyDataSetChanged();
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }


    }

    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_hot_fragment);

    }

    @Override
    public void onClick(View v) {
        if(v!=null) {
            Integer position = (Integer) v.getTag();
            Intent intent = new Intent(getActivity(),SpecialActivity.class);
            HotMonkeyModel hotMonkeyModel = mData.get(position);
            intent.putExtra("model",hotMonkeyModel);
            startActivity(intent);
        }
    }
}
